import QtQuick 2.15
import QtQuick.Controls 2.15
import '../../controls'
//统计查询 -- 管理
Rectangle {
    anchors.fill: parent
    gradient: mainGradient
    property int pageNo: 0
    property int pageSize: 50
    property bool loading: false
    property bool finished: true
    Component.onCompleted: {
        let tempDate = new Date();
        let tempDate2= new Date();
        tempDate.setDate(tempDate.getDate() - 1);
        txtDate1.text=tempDate.toLocaleString(Qt.locale("de_DE"),"yyyy-MM-dd")
        txtDate1.shortText=tempDate.toLocaleString(Qt.locale("de_DE"),"yyMM")
        txtDate2.text=tempDate2.toLocaleString(Qt.locale("de_DE"),"yyyy-MM-dd")
        txtDate2.shortText=tempDate2.toLocaleString(Qt.locale("de_DE"),"yyMM")
        finished=false
        pageNo=1
        loadData()
    }

    function loadData()
    {
        if(pageNo <= 1)
        {
            lvMode.clear()
        }

        loading=true
        var request = HttpHelper.createIMRequest();
        // request.enabledLog=true
        request.onSucceed( function(request){
            console.log('----water_get_cost_record---',JSON.stringify(request.responseData))
            finished=request.responseData.list.length === 0
            for(let i=0;i<request.responseData.list.length;i++)
            {
                let rdata=request.responseData.list[i]
                lvMode.append({ddata:rdata})
            }
            loading=false
        } );
        request.onError(  function(request){
            loading=false
            CGlobal.showMessage(window,request.errorMessage)
        } );
        request.get("/sys/watpc/pc/water_get_cost_record?page="+pageNo+'&limit='+pageSize+'&start='+txtDate1.text+'&end='+txtDate2.text)
    }
    ListModel {
        id:lvMode
    }
    Item {
        id:itemTop
        width: parent.width
        height: 80
        Row{
            spacing: 10
            anchors.centerIn: parent
            Text {
                color: '#ffffff'
                anchors.verticalCenter: parent.verticalCenter
                text: qsTr("开始时间")
            }
            Button{
                id:btnCalendarStart
                width: 160
                height: 40
                background: Rectangle{
                    border.width: 1
                    border.color: '#C7CDD8'
                    radius: 6
                    Text {
                        id:txtDate1
                        x:15
                        property var shortText: ''
                        anchors.verticalCenter: parent.verticalCenter
                        color: '#000000'
                        font.pixelSize: 18
                        Component.onCompleted: {
                            //                                            txtDate1.text=(new Date(new Date() - 30*24*60*60*1000)).toLocaleString(Qt.locale("de_DE"),"yyyy-MM-dd")
                        }
                    }
                    Image {
                        anchors.right: parent.right
                        anchors.rightMargin: 10
                        anchors.verticalCenter: parent.verticalCenter
                        source: "qrc:/images/control/candel.png"
                    }
                }
                CCalendarUI{
                    id:pcalendar
                }
                function calendarDate(currentDate,shortDate){
                    txtDate1.text=currentDate
                    txtDate1.shortText=shortDate
                }
                onClicked: {
                    pcalendar.dateSelectCallBack=calendarDate
                    pcalendar.show(new Date(txtDate1.text))
                }

            }
            Item {
                width: 50
                height: 1
            }
            Text {
                color: '#ffffff'
                anchors.verticalCenter: parent.verticalCenter
                text: qsTr("结束时间")
            }
            Button{
                id:btnCalendarEnd
                width: 160
                height: 40
                background: Rectangle{
                    border.width: 1
                    border.color: '#C7CDD8'
                    radius: 6
                    Text {
                        id:txtDate2
                        x:15
                        property var shortText: ''
                        anchors.verticalCenter: parent.verticalCenter
                        color: '#000000'
                        font.pixelSize: 18
                        Component.onCompleted: {
                            //                                            txtDate1.text=(new Date(new Date() - 30*24*60*60*1000)).toLocaleString(Qt.locale("de_DE"),"yyyy-MM-dd")
                        }
                    }
                    Image {
                        anchors.right: parent.right
                        anchors.rightMargin: 10
                        anchors.verticalCenter: parent.verticalCenter
                        source: "qrc:/images/control/candel.png"
                    }
                }
                CCalendarUI{
                    id:pcalendar2
                }
                function calendarDate(currentDate,shortDate){
                    txtDate2.text=currentDate
                    txtDate2.shortText=shortDate
                }
                onClicked: {
                    pcalendar2.dateSelectCallBack=calendarDate
                    pcalendar2.show(new Date(txtDate2.text))
                }

            }
            Item {
                width: 50
                height: 1
            }
            Button{
                width: 120
                height: 40
                enabled: !loading
                background: Rectangle{
                    radius: 10
                    color: '#EA9508'
                    Text {
                        anchors.centerIn: parent
                        color: '#ffffff'
                        text: qsTr("查询")
                    }
                }
                onClicked: {
                    pageNo=1
                    loadData()
                }
            }
        }
    }
    Rectangle {
        border.color: '#dddddd'
        anchors.fill: parent
        anchors.topMargin: 100
        anchors.leftMargin: 20
        anchors.bottomMargin: 20
        anchors.rightMargin: 20
        Rectangle {
            id:itemHeader
            width: parent.width - 2
            anchors.horizontalCenter: parent.horizontalCenter
            height: 40
            color: '#F5F7FA'
            property var itemWidth: width / 9
            Row{
                anchors.verticalCenter: parent.verticalCenter
                height: parent.height
                width: parent.width
                Repeater{
                    model: [qsTr('编号'),qsTr('用户头像'),qsTr('用户昵称'),qsTr('订单编号'),qsTr('手环号'),qsTr('金额'),qsTr('兑换时间'),qsTr('归还时间'),qsTr('状态')]
                    Text {
                        width: itemHeader.itemWidth
                        height: itemHeader.height
                        color: '#E0000000'
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text: modelData
                        elide: Text.ElideRight
                        anchors.verticalCenter: parent.verticalCenter
                    }
                }
            }
        }
        ListView{
            id:lvs
            anchors.fill: parent
            anchors.topMargin: itemHeader.height
            model: lvMode
            ScrollBar.vertical: ScrollBar{}
            clip: true
            property var itemWidth: width / 9
            property var itemHeight: 40
            boundsBehavior: Flickable.StopAtBounds
            delegate: Rectangle{
                width: lvs.width
                height: lvs.itemHeight
                color: index % 2 === 0 ? '#FFF2F2F2' : '#ffffff'
                Row{
                    Text {
                        width: lvs.itemWidth
                        height: lvs.itemHeight
                        color: '#333333'
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text: ddata.id
                        elide: Text.ElideRight
                    }
                    Item {
                        width: lvs.itemWidth
                        height: lvs.itemHeight
                        AvatarItem{
                            width: lvs.itemHeight - 10
                            height: lvs.itemHeight - 10
                            source: {
                                if('avatar' in ddata && ddata.avatar)
                                {
                                    return ddata.avatar
                                }
                                return ''
                            }
                            anchors.centerIn: parent
                        }
                    }
                    Text {
                        width: lvs.itemWidth
                        height: lvs.itemHeight
                        color: '#333333'
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text: ddata.w_nickname
                        elide: Text.ElideRight
                        wrapMode: Text.WrapAnywhere
                    }
                    Text {
                        width: lvs.itemWidth
                        height: lvs.itemHeight
                        color: '#333333'
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text: ddata.w_no
                        elide: Text.ElideRight
                        wrapMode: Text.WrapAnywhere
                    }
                    Text {
                        width: lvs.itemWidth
                        height: lvs.itemHeight
                        color: '#333333'
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text: ddata.b_name
                        elide: Text.ElideRight
                        wrapMode: Text.WrapAnywhere
                    }
                    Text {
                        width: lvs.itemWidth
                        height: lvs.itemHeight
                        color: '#333333'
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text: ddata.total.toFixed(2)
                        elide: Text.ElideRight
                    }
                    Text {
                        width: lvs.itemWidth
                        height: lvs.itemHeight
                        color: '#333333'
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text: ddata.create_time
                        elide: Text.ElideRight
                        wrapMode: Text.WrapAnywhere
                    }
                    Text {
                        width: lvs.itemWidth
                        height: lvs.itemHeight
                        color: '#333333'
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text: ddata.back_time
                        elide: Text.ElideRight
                        wrapMode: Text.WrapAnywhere
                    }
                    Text {
                        width: lvs.itemWidth
                        height: lvs.itemHeight
                        color: '#333333'
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text: {
                            switch(ddata.p_status)
                            {
                            case 0:
                                return qsTr('待支付')
                            case 1:
                                return qsTr('已支付')
                            case 2:
                                return qsTr('已退款')
                            case 3:
                                return qsTr('已取消')
                            }
                            return  qsTr('未知类型:')+ddata.p_status
                        }

                        elide: Text.ElideRight
                    }

                }
            }
            onAtYEndChanged: {
                if(atYEnd && !loading && !finished)
                {
                    pageNo++
                    console.log('-------load page-----------',pageNo)

                    loadData()
                }
            }
        }
    }
}
